﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
' http://dnntweet.codeplex.com
'
' Copyright (c) 2011, Will Strohl
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this 
' software and associated documentation files (the "Software"), to deal in the Software 
' without restriction, including without limitation the rights to use, copy, modify, 
' merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
' permit persons to whom the Software is furnished to do so, subject to the following 
' conditions:
'
' The above copyright notice and this permission notice shall be included in all copies 
' or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
' PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
' HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
' OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'

Option Infer On

Imports System.Collections.Generic
Imports System.Text
Imports System.Xml.Linq

Namespace WillStrohl.Modules.Twitter

    <Serializable()> _
    Public NotInheritable Class SearchAPI

        Private Const urlTemplate As String = "http://search.twitter.com/search.atom?q={0}"
        Private Shared atomNS As XNamespace = "http://www.w3.org/2005/Atom"

        Public Shared Function Query(ByVal SearchText As String) As List(Of TweetInfo)

            Dim xDoc As XDocument = XDocument.Load(String.Format(urlTemplate, SearchText))

            Dim tweets = From tweet In xDoc.Descendants(atomNS + "entry") _
                Select New TweetInfo With { _
                    .Id = CType(tweet.Element(atomNS + "id"), String), _
                    .Published = DateTime.Parse(CType(tweet.Element(atomNS + "published"), String)), _
                    .Title = CType(tweet.Element(atomNS + "title"), String), _
                    .Link = tweet.Elements(atomNS + "link") _
                        .Where(Function(link) CType(link.Attribute("rel"), String) = "alternate") _
                        .Select(Function(link) CType(link.Attribute("href"), String)).First(), _
                    .Image = tweet.Elements(atomNS + "link") _
                        .Where(Function(link) CType(link.Attribute("rel"), String) = "image") _
                        .Select(Function(link) CType(link.Attribute("href"), String)).First(), _
                    .Author = (From author In tweet.Descendants(atomNS + "author") _
                        Select New AuthorInfo With { _
                            .Name = CType(author.Element(atomNS + "name"), String), _
                            .Uri = CType(author.Element(atomNS + "uri"), String) _
                        }).First() _
                }

            If tweets.Count > 0 Then

                Dim lstTweet As New List(Of TweetInfo)
                Dim oTweet As TweetInfo = Nothing
                For Each tweet In tweets
                    oTweet = New TweetInfo
                    With oTweet
                        .Author = New AuthorInfo
                        .Author = tweet.Author
                        .Id = tweet.Id
                        .Link = tweet.Link
                        .Published = tweet.Published
                        .Title = tweet.Title
                        .Image = tweet.Image
                    End With
                    lstTweet.Add(oTweet)
                Next

                Return lstTweet

            Else

                Return Nothing

            End If

        End Function

        Public Shared Function GetSearchResults(ByVal SearchTerms As String, ByVal AuthorSaidText As String) As String

            If Not String.IsNullOrEmpty(SearchTerms) Then

                Dim tweets As List(Of TweetInfo) = Query(SearchTerms)

                If Not tweets Is Nothing AndAlso tweets.Count > 0 Then

                    Dim sb As New StringBuilder(100)

                    For Each tweet As TweetInfo In tweets

                        sb.Append("<div class=""dnntweet-Tweet ui-accordion ui-widget ui-helper-reset ui-accordion-icons"">")
                        sb.AppendFormat("<h3 class=""dnntweet-Author ui-accordion-header ui-helper-reset ui-state-default ui-corner-all"">{0} {1}</h3>", tweet.Author.Name, AuthorSaidText)
                        sb.Append("<div class=""dnntweet-TweetWrapOuter ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active"">")
                        sb.AppendFormat("<span class=""dnntweet-ImageWrap""><a href=""{1}"" target=""_blank""><img src=""{0}"" class=""dnntweet-Image"" /></a></span>", tweet.Image, tweet.Link)
                        sb.Append("<span class=""dnntweet-TweetWrap"">")
                        sb.AppendFormat("<span class=""dnntweet-Title"">{0}</span>", tweet.Title)
                        sb.Append("<span class=""dnntweet-PublishWrap"">")
                        sb.AppendFormat("<span class=""dnntweet-Author""><a href=""{1}"" target=""_blank"" class=""dnntweet-Link"">{0}</a></span>, ", tweet.Author.Name, tweet.Author.Uri)
                        sb.AppendFormat("<span class=""dnntweet-LinkOuter""><a href=""{1}"" target=""_blank"" class=""dnntweet-Link""><span class=""dnntweet-LinkInner"">{0}</span></a></span>", tweet.Published, tweet.Link)
                        sb.Append("</span>")
                        sb.Append("</span>")
                        sb.Append("</div>")
                        sb.Append("</div>")

                    Next

                    Return sb.ToString

                End If

            End If

            Return Null.NullString

        End Function

    End Class

End Namespace